package love.yuanmengrobot.springmvcdemo2.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import love.yuanmengrobot.springmvcdemo2.mapper.UserMapper;
import love.yuanmengrobot.springmvcdemo2.pojo.SysRole;
import love.yuanmengrobot.springmvcdemo2.pojo.SysUser;
import love.yuanmengrobot.springmvcdemo2.service.UserService;
import love.yuanmengrobot.springmvcdemo2.utils.DataGridView;
import love.yuanmengrobot.springmvcdemo2.utils.SystemConstant;
import love.yuanmengrobot.springmvcdemo2.vo.UserVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Resource
//    @Autowired
    private UserMapper userMapper;
    /**
     * 登录
     * @param userVo
     */
    public SysUser login(UserVo userVo) {
        //加密 进行比较
        String pwd= DigestUtils.md5DigestAsHex(userVo.getPwd().getBytes());
        userVo.setPwd(pwd);
        SysUser user = this.userMapper.login(userVo);
        return user;
    }
    @Override
    public DataGridView queryNameAndEmail(UserVo userVo) {
        Page<SysUser> page= PageHelper.startPage(userVo.getPage(),userVo.getLimit());
        List<SysUser> list= this.userMapper.queryNameAndEmail(userVo);
        return new DataGridView(page.getTotal(),list);
    }

    @Override
    public DataGridView queryAllAdministrators(UserVo userVo) {
        Page<SysUser> page= PageHelper.startPage(userVo.getPage(),userVo.getLimit());
        List<SysUser> list= this.userMapper.queryAllAdministrators(userVo);
        return new DataGridView(page.getTotal(),list);
    }


    @Override
    public DataGridView queryAllUser(UserVo userVo) {
        Page<SysUser> page= PageHelper.startPage(userVo.getPage(),userVo.getLimit());
        List<SysUser> list= this.userMapper.queryAllUser(userVo);
        return new DataGridView(page.getTotal(),list);
    }

    @Override
    public void addUser(UserVo userVo) {
        userVo.setType(SystemConstant.USER_TYPE_NORMAL);
        userVo.setPwd(DigestUtils.md5DigestAsHex(SystemConstant.DEFAULT_PASSWORD.getBytes()));
        this.userMapper.insertSelective(userVo);
    }

    @Override
    public void updateUser(UserVo userVo) {
        this.userMapper.updateByPrimaryKeySelective(userVo);
    }

    @Override
    public void deleteUser(Integer id) {
        this.userMapper.deleteByPrimaryKey(id);
    }


    @Override
    public void resetPwdUser(Integer id) {
        SysUser user=this.userMapper.selectByPrimaryKey(id);
        user.setPwd(DigestUtils.md5DigestAsHex(SystemConstant.DEFAULT_PASSWORD.getBytes()));
        this.userMapper.updateByPrimaryKeySelective(user);
    }
    @Override
    public SysUser queryUserLoginnameAndUserid(String id) {
        SysUser user=this.userMapper.selectByLoginnameAndUserid(id);
        return user;
    }
    @Override
    public List<SysRole> queryUserHaveRole(Integer id) {
        return this.userMapper.queryUserHaveRole(id);
    }

    @Override
    public void saveSelectRole(Integer uid, Integer[] ids) {
        //1，先删除对应的用户的角色 再重新添加
        this.userMapper.deleteRoleAndUserByUid(uid);
        //2.保存用户分配的角色
        for (Integer id : ids) {
            this.userMapper.saveSelectRole(uid,id);
        }

    }

    @Override
    public void updateUser2(SysUser user) {
        this.userMapper.updateByPrimaryKeySelective(user);
    }
}
